<?php

require_once "Smarty/Smarty.class.php";
require_once "config.class.php";

class Response
{
    private $_view;    
    private $_exceptions = array();
    private $_G_title = '';
    private $_G_css_files = array();
    private $_G_js_files = array();
    private $_G_ie_css_files = array();
    //private $_err_msg = array();
    private $_G_err_msgs = array();
    private $_G_info_msgs = array();
    
    public function Response () {
        $this->initView();
        $cfg = Config::getInstance();
        $this->setTitle($cfg->title);
        foreach ($cfg->jsfiles as $item) {
            $this->appendJs($item);
        }
        foreach ($cfg->cssfiles as $item) {
            $this->appendCss($item);
        }
        foreach ($cfg->iecssfiles as $item) {
            $this->appendIeCss($item);
        }

    }
    protected function initView () {
        $this->_view = new Smarty();
        $this->_view->compile_check = true;
        $this->_view->template_dir = APP_PATH . "/view";
        $this->_view->compile_dir = LIB_PATH . "/temp";
        $this->_view->cache_dir = LIB_PATH . "/temp";
        $this->_view->plugins_dir = array("plugins", "../smarty_plugins");
        $this->_view->left_delimiter = "{{";
        $this->_view->right_delimiter = "}}";
        
    }

    # get resources file url
    public function res ($file) {
        $cfg = Config::getInstance();
        return $cfg->resurl . $file;
    }

    # redirect
    public function redirect ($url) {
        Header("Location: $url");
    }

    # exceptions
    public function appendException ($e) {
        $this->_exceptions[] = $e;
    }
    public function exceptions () {
        return $this->_exceptions;
    }

    # properties
    public function view () {
        return $this->_view;
    }

    public function setTitle ($title) {
        $this->_G_title = $title;
    }

    public function appendErrMsg ($msg) {
        $this->_G_err_msgs[] = $msg;
    }
    public function hasErrMsg () {
        return count($this->_G_err_msgs) != 0;
    }


    public function appendCss ($cssfile, $res=True) {
        if ($res) {
            $cssfile = $this->res($cssfile);
        }
        $this->_G_css_files[] = $cssfile;
    }
    public function appendJs ($jsfile, $res=True) {
        if ($res) {
            $jsfile = $this->res($jsfile);
        }
        $this->_G_js_files[] = $jsfile;
    }
    public function appendIeCss ($cssfile, $res=True) {
        if ($res) {
            $cssfile = $this->res($cssfile);
        }
        $this->_G_ie_css_files[] = $cssfile;
    }

    public function appendInfoMsg ($msg) {
        $this->_G_info_msgs[] = $msg;
    }

    public function render ($tpl) {
        $cfg = Config::getInstance();
        $this->_view->assign('G_title', $this->_G_title);
        $this->_view->assign('G_css_files', $this->_G_css_files);
        $this->_view->assign('G_js_files', $this->_G_js_files);
        $this->_view->assign('G_ie_css_files', $this->_G_ie_css_files);
        $this->_view->assign('G_res', $cfg->resurl);
        $this->_view->assign('G_baseurl', $cfg->baseurl);
        $this->_view->assign('G_err_msg', $this->_G_err_msgs);
        $this->_view->assign('G_infos_msg', $this->_G_info_msgs);
        $this->_view->display($tpl);
    }

    
}

